package me.winds.test;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.ViewGroup;


import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import me.winds.test.widget.WheelView;
import me.winds.wrapper.ViewHelper;
import me.winds.wrapper.WrapperDialog;


/**
 * Auther:  winds
 * Email:   heardown@163.com
 * Date:    2018/8/6.
 * Desc:
 */
public class AreaSelector extends WrapperDialog {

    public AreaSelector(Context context, List<CityVo> list) {
        super(context, R.style.BottomTheme);
        dialog.getWindow().setWindowAnimations(R.style.BottomAnim);
        this.data = list;
        init();
    }


    @Override
    public int getLayoutRes() {
        return R.layout.dialog_area_selector;
    }

    @Override
    protected void setDialogParams(Dialog dialog) {
        setDialogParams(dialog, ViewGroup.LayoutParams.MATCH_PARENT, AutoUtils.getPercentHeightSize(750), Gravity.BOTTOM);
    }

    @Override
    public void help(final ViewHelper helper) {
        final WheelView wvProvince = helper.getView(R.id.wv_province);
        final WheelView wvCity = helper.getView(R.id.wv_city);
        wvProvince.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                wvCity.setItems(getCityString(selectedIndex));
                wvCity.setSeletion(0);
            }
        });

    }

    private void init() {
        if (helper != null) {
            final WheelView wvProvince = helper.getView(R.id.wv_province);
            final WheelView wvCity = helper.getView(R.id.wv_city);
            wvProvince.setOffset(2);
            wvCity.setOffset(2);
            wvProvince.setItems(getProvinceString());
            wvCity.setItems(getCityString(0));

        }
    }

    List<CityVo> data;
    int provinceIndex;


    public List<String> getProvinceString() {
        List<String> list = new ArrayList<>();
        if (data != null && data.size() > 0) {
            for (int i = 0; i < data.size(); i++) {
                list.add(data.get(i).name);
            }
        }
        return list;
    }

    public List<String> getCityString(int index) {
        provinceIndex = index;
        List<String> list = new ArrayList<>();
        if (data != null && data.size() > 0) {
            if (provinceIndex >= data.size()) {
                provinceIndex = data.size() - 1;
            }
            List<String> cities = data.get(provinceIndex).cities;
            if (cities != null && cities.size() > 0) {
                list.addAll(cities);
            }
        }
        return list;
    }

    public String getSelectedCity(int cityIndex) {
        CityVo cityVo = data.get(provinceIndex);
        if (cityVo != null && cityVo.cities != null && cityVo.cities.size() > 0) {
            if (cityIndex >= cityVo.cities.size()) {
                cityIndex = cityVo.cities.size() - 1;
            }
            return cityVo.cities.get(cityIndex);
        }
        return null;
    }

    public String getAreaSelected() {
        if (data != null && helper != null) {
            final WheelView wvCity = helper.getView(R.id.wv_city);
            int seletedIndex = wvCity.getSeletedIndex();
            return getSelectedCity(seletedIndex);
        }
        return null;
    }
}
